package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.decoration.DividerItemDecoration;
import com.eduhzy.ttw.teacher.mvp.model.entity.ChildAreaData;
import com.eduhzy.ttw.teacher.mvp.model.entity.ChooseSchoolData;
import com.eduhzy.ttw.commonsdk.entity.MineClassData;
import com.eduhzy.ttw.commonsdk.entity.SchoolTypeData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerChooseSchoolComponent;
import com.eduhzy.ttw.teacher.mvp.contract.ChooseSchoolContract;
import com.eduhzy.ttw.teacher.mvp.presenter.ChooseSchoolPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.mcxtzhang.indexlib.IndexBar.widget.IndexBar;
import com.mcxtzhang.indexlib.suspension.SuspensionDecoration;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-19 10:06:17
 */
@Route(path = RouterHub.APP_CHOOSESCHOOLACTIVITY)
public class ChooseSchoolActivity extends BaseActivity<ChooseSchoolPresenter> implements ChooseSchoolContract.View {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.public_toolbar_right_tv_layout)
    RelativeLayout right;
    @BindView(R.id.indexBar)
    IndexBar indexBar;
    @BindView(R.id.tvSideBarHint)
    TextView tvSideBarHint;
    @Autowired(name = Constants.ROUTER_DATA)
    ChildAreaData areaData ;
    @Autowired(name = Constants.ROUTER_INTEGER)

    @Inject
    BaseQuickAdapter<ChooseSchoolData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager layoutManager;
    @Inject
    DividerItemDecoration dividerItemDecoration;
    @Inject
    SuspensionDecoration suspensionDecoration;

    private QMUITipDialog tipDialog;
    private ChooseSchoolData data;
    private String addressAreaName;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerChooseSchoolComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_choose_school;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        right.setVisibility(View.VISIBLE);
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        rvList.addItemDecoration(suspensionDecoration);
        rvList.addItemDecoration(dividerItemDecoration);
        indexBar.setmPressedShowTextView(tvSideBarHint)//设置HintTextView
                .setNeedRealIndex(true)//设置需要真实的索引
                .setmLayoutManager((LinearLayoutManager) layoutManager);
        mPresenter.getGroupbyAreaMap(areaData.getAreaCode());
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            data = mAdapter.getData().get(position);
            if (data.getId().equals(SPUtils.getInstance().getString(Constants.GROUPID))) {
                Message msg = Message.obtain();
                msg.what = EventBusTags.CHOOSE_SCHOOL_SUCCESS;
                EventBus.getDefault().post(msg, RouterHub.APP_MINESCHOOLACTIVITY);
                killMyself();
                return;
            }
            mPresenter.getClassByUser();
//            ARouter.getInstance().build(RouterHub.APP_MINESCHOOLACTIVITY)
//                    .withInt(Constants.ROUTER_INTEGER, type+1)
//                    .withString(Constants.ROUTER_STRING, data.getAreaCode())
//                    .withString(Intent.EXTRA_TITLE, Constants.ChooseAreaTitle.getTitle(type+1))
//                    .navigation();

        });
    }

    @Override
    public void showLoading() {
        if (tipDialog == null) {
            tipDialog = RxUtil.showLoading(this, "加载中···");
        }
        tipDialog.show();
    }

    @Override
    public void hideLoading() {
        if (ObjectUtils.isNotEmpty(tipDialog)) {
            tipDialog.dismiss();
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void getClassData(List<MineClassData> list) {
        if (list.isEmpty()) {
            mPresenter.showJoinSchoolDialog(data);
        } else {
            List<MineClassData> bzrList = new ArrayList<>();
            List<MineClassData> normalList = new ArrayList<>();
            for (MineClassData classData : list) {
                if (classData.isBzr()) {
                    bzrList.add(classData);
                } else {
                    normalList.add(classData);
                }
            }
            if (ObjectUtils.isNotEmpty(bzrList)) {
                mPresenter.showTransferDialog(bzrList);
            } else if (ObjectUtils.isNotEmpty(normalList)) {
                mPresenter.showDropClassDialog(normalList, data);
            }
        }
    }


    @Override
    public void update(List<ChooseSchoolData> list) {
        addressAreaName = list.get(0).getAddressAreaName();
        getWindow().getDecorView().postDelayed(() -> {
            mAdapter.setNewData(list);
            mAdapter.notifyDataSetChanged();
            indexBar.setmSourceDatas(list).invalidate();
            suspensionDecoration.setmDatas(list);
        }, 500);
    }

    @Override
    public void getSchoolType(List<SchoolTypeData> data) {
        if (ObjectUtils.isNotEmpty(areaData)&&ObjectUtils.isNotEmpty(addressAreaName)) {
            mPresenter.showAddSchool(data, areaData,addressAreaName);
        }
    }

    @OnClick(R.id.public_toolbar_right_tv_layout)
    public void onViewClicked() {
        mPresenter.getBaseCodeByType();
    }
}
